package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFile;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.enumeration.OperationType;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

@Service
public class SetmealService implements com.sky.service.SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        Long setmealid = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(new Consumer<SetmealDish>() {
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(setmealid);
            }
        });

        setmealDishMapper.insertBatch(setmealDishes);


    }

    @Override
    public PageResult pagequery(SetmealPageQueryDTO setmealPageQueryDTO) {
        int pageNum = setmealPageQueryDTO.getPage();
        int pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(pageNum, pageSize);
        Page<SetmealVO> page = (Page<SetmealVO>)setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void deletebatch(List<Long> ids) {
        ids.forEach(new Consumer<Long>() {

                        @Override
                        public void accept(Long id) {
                            Setmeal setmeal =setmealMapper.getbyid(id);
                            if(setmeal.getStatus()== StatusConstant.ENABLE){
                                throw new RuntimeException(MessageConstant.DISH_ON_SALE);
                            }
                        }
                    }
        );
        //删除套餐
        setmealMapper.deletebyids(ids);
        //删除套餐菜品关系中的数据
        setmealDishMapper.deletebyids(ids);


    }

    @Override
    public SetmealVO getsetmealbyid(Long id) {
        Setmeal setmeal = setmealMapper.getbyid(id);
        List<SetmealDish> setmealdish=setmealDishMapper.getbysetmealid(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealdish);

        return setmealVO;
    }

    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //套餐id
        Long id = setmealDTO.getId();
        List<Long> ids=new ArrayList<>();
        ids.add(id);
        setmealDishMapper.deletebyids(ids);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(new Consumer<SetmealDish>() {
            @Override
            public void accept(SetmealDish setmealDish) {
                setmealDish.setSetmealId(id);
            }
        });

        setmealDishMapper.insertBatch(setmealDishes);


    }

    @Override
    public void startorstop(Integer status, Long id) {
        //判断是否有停售菜品，有的话无法起售
        if(status==StatusConstant.ENABLE){
            List<Dish> dishList=dishMapper.getdishbysetmealid(id);
            if(dishList.size()>0&&dishList!=null){
                dishList.forEach(new Consumer<Dish>() {
                    @Override
                    public void accept(Dish dish) {
                        Integer status1 = dish.getStatus();
                        if(status1==StatusConstant.DISABLE){
                            throw new RuntimeException(MessageConstant.SETMEAL_ENABLE_FAILED);
                        }
                    }
                });
            }
        }

            Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
            setmealMapper.update(setmeal);



    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }


}
